$(document).ready(function (event) {


    $.ajax({
        //IP定位
        url: "http://restapi.amap.com/v3/ip?key=c76df1373d9f8adca55f7f7bebae5f70",
        // https://restapi.amap.com/v3/geocode/geo?parameters
        success: function (result) {
            $(".postion").text(result.province + " " + result.city);
            $.ajax({
                url: "https://restapi.amap.com/v3/config/district?" + "keywords=" + "中华人民共和国" + "&subdistrict=3&key=c76df1373d9f8adca55f7f7bebae5f70",
                dataType: "json",
                type: "get",
                success: function (data) {
                    //国家
                    $.each(data.districts, function (index, country) {
                        //省级
                        $.each(country.districts, function (index, province) {

                            $("#province").append("<option class=''+value='" + province.name + "'>" + province.name + "</option>");
                            // var a="."+"a"+province.adcode;
                            $(function () {
                                $("#province").change(function () {
                                    $("#city").empty()
                                    var provinceName = this.value;
                                    if (provinceName == "-1") {
                                        $("#city").append("<option class='' value='-1' select;ed>" + "请选择您的收货城市" + "</option>");
                                    } else if (provinceName == province.name) {
                                        findCurrentProvince(provinceName);

                                    }
                                })
                            });
                        })
                    })

                    $("#city").change(function () {
                        $(".address").focus();

                    })
                    //
                }
            });
        }
    });

});

function findCurrentProvince(currentProvince) {

    $.ajax({
        url: "https://restapi.amap.com/v3/config/district?" + "keywords=" + currentProvince + "&subdistrict=3&key=c76df1373d9f8adca55f7f7bebae5f70",
        dataType: "json",
        type: "get",
        success: function (provinceResult) {
            $.each(provinceResult.districts, function (index, province) {
                $.each(province.districts, function (index, city) {
                    $("#city").append("<option value=''>" + city.name + "</option>")
                })
            })
        }
    });
}
